package com.game.screen;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.game.GameJam;
import com.game.screen.level.GameMap;
import com.game.screen.level.Obstacle;
import com.game.screen.level.SpriteManager;
import com.game.util.ArrayRenderer;
import com.game.util.Coord;

public class GameScreen extends Screen{

	public static float GRAVITY = -0.000001f;
	public static int time = 300;
	private Player player1 = new Player("cloud.png", 100, 300);
	private Obstacle obstacle1 = new Obstacle("obstacle1.png", 140, 134);
	private Obstacle obstacle2 = new Obstacle("obstacle1.png", 80, 84);
	private Obstacle obstacle3 = new Obstacle("obstacle1.png", 40, 34);
	private Collision collision= new Collision();
	public Background bg;
	private Coord newPosition;
	private List tiles = new ArrayList();
	private ArrayRenderer renderer;
	private SpriteManager manager;
	private GameMap map;
	
	public GameScreen(){
		tiles.add(new Tile(200,200));
		
		bg = new Background("bg1.jpg", 0, 0);

		resetPlayer();
	}
	
	public void resetPlayer(){
		player1 = new Player("cloud.png", 100, 300);
		Player.asleep = true;
		Player.numBubbles = 2;

		bg.reset(0, 0);

		manager = new SpriteManager();
		
		try {
			map = manager.loadMap("/data/level1.txt");
		} catch (IOException ioe) {
			System.out.println ("GameScreen.java: Exception while loading map" + ioe.toString());
		}
	}
	
	public void drawBox() {
	
		//super.drawBox();
		
		bg.display();
		
		if(Player.asleep){
			map.displayMap();
		}
		else{
			if(Player.numBubbles<7){			
				GameJam game = ((GameJam)pApplet);
				game.changeScreen(GameJam.GAME_OVER_SCREEN_OK);
			}
			else if(Player.numBubbles>=7 && Player.numBubbles<13 ){
				GameJam game = ((GameJam)pApplet);
				game.changeScreen(GameJam.GAME_OVER_SCREEN_GREAT);
			}
			else {
				GameJam game = ((GameJam)pApplet);
				game.changeScreen(GameJam.GAME_OVER_SCREEN_PERFECT);
			}

		}
		player1.display();
//		obstacle1.display();
//		obstacle2.display();
//		obstacle3.display();
		
		if(collision.isCollision(player1,obstacle1)){System.out.println("collides here with 1");}
		if(collision.isCollision(player1,obstacle2)){System.out.println("collides here with 2");}
		if(collision.isCollision(player1,obstacle3)){System.out.println("collides here with 3");}
		
	}

	public void onKeyPressed(char key, int keyCode){
		player1.onKeyPressed(key, keyCode);
		/*
		player1.onKeyPressed(key, keyCode);
		System.out.println(player.getPosition()+" and the box is at "+collision.getPosition());
		//collision.checkCollision();
		if(player.getPosition().getX()-20<collision.getPosition().getX()+70 && player.getPosition().getX()> collision.getPosition().getX()){
			System.out.println("I am here");
			if(player.getPosition().getY()+20==collision.getPosition().getY()){
				System.out.println("collsiion above the box");
				newPosition=new Coord(player.getPosition().getX(),player.getPosition().getY()-12);
				player.setPosition(newPosition);
			}
			else if(player.getPosition().getY()-20==collision.getPosition().getY()+70){
				System.out.println("collsiion on bottom of box");
				newPosition=new Coord(player.getPosition().getX(),player.getPosition().getY()+12);
				player.setPosition(newPosition);
			}
		}
		else if(player.getPosition().getY()+20>collision.getPosition().getY() && player.getPosition().getY()-20< collision.getPosition().getY()+70){
			System.out.println("I am here");
			if(player.getPosition().getX()-20==collision.getPosition().getX()+70){
				System.out.println("collsiion on left side of box");
				newPosition=new Coord(player.getPosition().getX()+12,player.getPosition().getY());
				player.setPosition(newPosition);
			}
			else if(player.getPosition().getX()+20==collision.getPosition().getX()){
				System.out.println("collsiion on right side of box");
				newPosition=new Coord(player.getPosition().getX()-12,player.getPosition().getY());
				player.setPosition(newPosition);
			}
		}*/
	}
	
}
